package com.maxlen.camunda.listener;

import com.maxlen.camunda.controller.WebSocketController;
import com.maxlen.camunda.service.CamundaService;
import com.tcloudit.common.core.domain.R;
import com.tcloudit.system.api.RemoteThirdPartyService;
import com.tcloudit.system.api.RemoteUserService;
import com.tcloudit.system.api.domain.CamundaUserPost.EmailDetailTO;
import com.tcloudit.system.api.domain.CamundaUserPost.UserEmailTO;
import org.camunda.bpm.engine.delegate.DelegateTask;
import org.camunda.bpm.engine.delegate.TaskListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class MyTaskListener implements TaskListener {

    private static final Logger logger = LoggerFactory.getLogger(MyTaskListener.class);

    @Autowired
    private RemoteUserService remoteUserService;
    @Autowired
    private RemoteThirdPartyService remoteThirdPartyService;

    @Override
    public void notify(DelegateTask delegateTask) {
        String businessKey = delegateTask.getExecution().getBusinessKey();
        String assignee = delegateTask.getAssignee();
        System.out.println(businessKey);

        logger.info("收到来自：{}\n", businessKey);
        logger.info("任务分配给：{}\n", assignee);
        List<String> userIdList = new ArrayList<>();
        userIdList.add(businessKey);
        userIdList.add(assignee);

        R<List<UserEmailTO>> userNameEmailByUserId = remoteUserService.getUserNameEmailByUserIdList(userIdList);
        if(R.isSuccess(userNameEmailByUserId)){
            List<UserEmailTO> userEmailTOList = userNameEmailByUserId.getData();
            for (UserEmailTO userEmailTO : userEmailTOList) {
                if(userEmailTO.getUserId().equals(businessKey)){
                    WebSocketController.sendMessage(assignee, "收到来自 " + userEmailTO.getUserName() + " 的请假申请");
                } else {
                    if(userEmailTO.getUserName() != null && userEmailTO.getEmail() != null){
//                        String builder = "您有一条来自" + userEmailTO.getUserName() + "的请假审核，请处理" +
//                                "\n<a href=\"http://localhost/camunda/AFL-process\" type=\"primary\" target=\"_blank\">点击这里开始审核</a>";
//                        EmailDetailTO emailDetailTO = new EmailDetailTO(userEmailTO.getUserName(), userEmailTO.getEmail(), builder);
//
//                        R<String> stringR = remoteThirdPartyService.sendEmail(emailDetailTO);
//                        if(R.isSuccess(stringR)){
//                            logger.info("发送邮件给：{}", userEmailTO.getUserName());
//                            logger.info("邮件：{}\n", userEmailTO.getEmail());
//                        } else {
//                            logger.error("发送邮件失败：{}\n", userEmailTO.getEmail());
//                        }
                    }
                }
            }
        }

//        Map<String, Object> variables = delegateTask.getVariables();
//        variables.forEach((k, v) -> {
//            logger.info("参数：{} ： {}",k ,v);
//        });
    }
}
